﻿using MediatR;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.OutboundCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.OutboundCommandHadler
{
    public class OutboundShowCommandHandler : IRequestHandler<OutboundShowCommand, List<OutboundDTO>>
    {
        private readonly  IOutboundRepository outboundRepository;

        private readonly IOutStorageProductsRepository outStorageProductsRepository;

        public OutboundShowCommandHandler(IOutboundRepository outboundRepository, IOutStorageProductsRepository outStorageProductsRepository)
        {
            this.outboundRepository = outboundRepository;
            this.outStorageProductsRepository = outStorageProductsRepository;
        }

        public async Task<List<OutboundDTO>> Handle(OutboundShowCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await outboundRepository.GetAllAsync()
                       join b in await outStorageProductsRepository.GetAllAsync() on a.Outboundid equals b.Outboundid
                       where a.IsDelete == false
                       select new OutboundDTO
                       {
                           Outboundid = a.Outboundid,
                           OutboundNo = a.OutboundNo,
                           OutboundType = a.OutboundType,
                           AssociateNumber = a.AssociateNumber,
                           CustomerNo = a.CustomerNo,
                           CustomerName = a.CustomerName,
                           CustomerContact = a.CustomerContact,
                           CustomerPhone = a.CustomerPhone,
                           SingleMaker = a.SingleMaker,
                           SingleTime = a.SingleTime,
                           OutboundRemarks = a.OutboundRemarks,
                           OutboundState=a.OutboundState,
                           ExamineReason=a.ExamineReason,
                           ExaminePeople=a.ExaminePeople,
                           ExamineTime=a.ExamineTime,
                           DeliveryTime=a.DeliveryTime,
                           office_id = a.office_id,
                           user_id = a.user_id,
                           OutboundidProductsId = b.OutboundidProductsId,
                           ProductName = b.ProductName,
                           ProductNo = b.ProductNo,
                           ProductSpec = b.ProductSpec,
                           Batch = b.Batch,
                           Price = b.Price,
                           OutStorageNum = b.OutStorageNum,
                           TotalPrice = b.TotalPrice,
                           BinLocationName = b.BinLocationName
                       };
            if (request.OutboundNo != "")
            {
                list = list.Where(w => w.OutboundNo == request.OutboundNo).ToList();
            }
            if (request.OutboundType != "")
            {
                list = list.Where(w => w.OutboundType.Contains(request.OutboundType)).ToList();
            }
            if (request.OutboundState != 0)
            {
                list = list.Where(w => w.OutboundState == request.OutboundState).ToList();
            }
            return list.ToList();
        }
    }
}
